package com.loubii.sunshineSchool.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.pojo.UserItem;
import com.loubii.sunshineSchool.ui.card.SSReasonActivity;
import com.loubii.sunshineSchool.util.ImageUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

public class SSMatesAdapter extends BaseRecycleAdapter {

    private final List<UserItem> mList;

    private Context context;
    private int bid;


    public SSMatesAdapter(Context context, List<UserItem> list, int bid) {
        this.context = context;
        this.mList = list;
        this.bid = bid;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemHoleder(LayoutInflater.from(context).inflate(R.layout.item_bill_list_for_select, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        super.onBindViewHolder(holder, position);

        ItemHoleder itemHoleder = (ItemHoleder) holder;
        itemHoleder.name.setText(mList.get(position).getUserName());
        ImageUtil.downloadAvatar(mList.get(position).getImage(), itemHoleder.pic);
        itemHoleder.describe.setText(mList.get(position).getSecondTitle());
        itemHoleder.reason.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SSReasonActivity.class);
                intent.putExtra("uid", mList.get(position).getUserId());
                intent.putExtra("bid", bid);
                context.startActivity(intent);
            }
        });
        itemHoleder.choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HttpBin.choosePerson(context, mList.get(position).getUserId(), bid, new HttpBin.ResponseListener<Boolean>() {
                    @Override
                    public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                        if (response.body()) {
                            Toast.makeText(context, "选择成功~", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(context, "你应经选择了参与者，活动已经完结啦~", Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Boolean> call, Throwable t) {

                    }
                });
            }
        });
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    class ItemHoleder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_classify)
        ImageView pic;
        @BindView(R.id.tv_classify)
        TextView name;
        @BindView(R.id.tv_classify_describe)
        TextView describe;
        @BindView(R.id.see_reason)
        Button reason;
        @BindView(R.id.choose_it)
        Button choose;

        public ItemHoleder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
